#ifndef __TABLE__
#define __TABLE__

#include "../lib/def.h"

#define DESC_G_4K 0x00800000
#define DESC_D_32 0x00400000
#define DESC_L 0x00200000
#define DESC_AVL 0x00100000
#define DESC_P 0x00008000
#define DESC_DPL_0 0x00000000
#define DESC_DPL_1 0x00002000
#define DESC_DPL_2 0x00004000
#define DESC_DPL_3 0x00006000
#define DESC_TYPE_SYSTEM 0x00000000
#define DESC_TYPE_CODE 0x00001800
#define DESC_TYPE_DATA 0x00001000
#define DESC_CODE_CONFIRMING 0x00000400
#define DESC_CODE_READABLE 0x00000200
#define DESC_CODE_ACCESSED 0x00000100
#define DESC_DATA_EXTEND_DIR 0x00000400
#define DESC_DATA_WRITABLE 0x00000200
#define DESC_DATA_ACCESSED 0x00000100

#define INTR_TYPE_INT16 0x06
#define INTR_TYPE_TRAP16 0x07
#define INTR_TYPE_INT32 0x0E
#define INTR_TYPE_TRAP32 0x0F
#define INTR_TYPE_TASK32 0x05
#define INTR_TYPE_CALL 0x0C

#define INTR_DPL_0 0x00
#define INTR_DPL_1 0x20
#define INTR_DPL_2 0x40
#define INTR_DPL_3 0x60

#define INTR_P 0x80

#define SEG_RPL0 0x0000
#define SEG_RPL1 0x0001
#define SEG_RPL2 0x0002
#define SEG_RPL3 0x0003

#define SEG_TI_GDT 0x0000
#define SEG_TI_LDT 0x0004

#define PG_P 0x00000001
#define PG_RW_W	0x00000002
#define PG_US_U	0x00000004
#define PG_ACC 0x00000020

typedef struct {
  WORD LimitLow;
  WORD BaseLow;
  BYTE BaseMid;
  BYTE Attribute1;
  BYTE LimitHighAndAttribute2;
  BYTE BaseHigh;
} GDT_DESC, *PGDT_DESC, *FAR LPGDT_DESC;

typedef struct {
  WORD OffsetLow;
  WORD Selector;
  BYTE Count;
  BYTE Attribute;
  WORD OffsetHigh;
} IDT_DESC, *PIDT_DESC, *FAR LPIDT_DESC;

typedef struct {
    DWORD Signature;
    DWORD dwPrivilege;
    DWORD cr3;
    HANDLE hFirstThread;
    DWORD dwAttributes;
} PROCESS_CONTEXT, *PPROCESS_CONTEXT, *FAR LPPROCESS_CONTEXT;

typedef struct {
    DWORD edi;
    DWORD esi;
    DWORD ebp;
    DWORD esp;
    DWORD ebx;
    DWORD edx;
    DWORD ecx;
    DWORD eax;
    DWORD gs;
    DWORD fs;
    DWORD es;
    DWORD ds;
    DWORD err;
    DWORD eip;
    DWORD cs;
    DWORD eflags;
    DWORD esp2;
    DWORD ss;
} CPU_CONTEXT, *PCPU_CONTEXT, *FAR LPCPU_CONTEXT;

typedef struct {
    FLOAT80 st[10];
    WORD fcw;
    WORD fsw;
    WORD fflags;
    WORD feppv;
    QWORD ffwptr;
} FPU_CONTEXT, *PFPU_CONTEXT, *FAR LPFPU_CONTEXT;

typedef union {
    QWORD qword[2];
    DWORD dword[4];
    WORD word[8];
    BYTE byte[16];
} XMMWORD, *PXMMWORD, *FAR LPXMMWORD;

typedef struct {
    DWORD Signature;
    CPU_CONTEXT cpu;
    FPU_CONTEXT fpu;
    XMMWORD xmm[8];
    HANDLE hProcess;
    HANDLE hPrevThread;
    HANDLE hNextThread;
    DWORD dwCreateTime;
    DWORD dwNextStartTime;
    DWORD dwAttributes;
} THREAD_CONTEXT, *PTHREAD_CONTEXT, *FAR LPTHREAD_CONTEXT;

#define PROCESS_SIGNATURE 0x00000001
#define THREAD_SIGNATURE 0x00000002

#define THREAD_BLOCKED 0x00000001

EXTERN LPVOID IDTTable[INTR_ENTRY_CNT];

VOID InitIdt(VOID);

#endif
